package com.strato.deploy.downloader.gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.strato.deploy.common.BundleInfo;
import com.strato.deploy.common.SiteInfo;

public class SitePanel extends JPanel {

	private static final long serialVersionUID = 6410862769546068996L;

	public static String SELECTED_VALUE="Y";

	private String siteName;
	private SiteInfo siteInfo;
	private JTable table;

	private GuiPanel guiPanel;
	
	public SitePanel(String siteName, SiteInfo site, GuiPanel gui) {
		super();
		this.siteName=siteName;
		this.siteInfo=site;
		this.guiPanel=gui;
		setLayout(new GridLayout(1,1));
		Object[] columnNames=new Object[]{"","Bundle Symbolic Name","Bundle Version"};
		DefaultTableModel model=new DefaultTableModel(columnNames,0){
			private static final long serialVersionUID = -485029119562946372L;
			@Override
			public boolean isCellEditable(int arg0, int arg1) {
				return false;
			}
		};
		table=new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int row = table.getSelectedRow();
				if(isSelected(row)){
					unselect(row);
				}else{
					select(row);
					guiPanel.cascadeSelectBundle(getBundleId(row), getBundleVersion(row));
				}
				guiPanel.updateTitle();
			}
		});
		table.setFillsViewportHeight(true);
		table.setRowSelectionAllowed(true);
		table.setCellSelectionEnabled(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setSelectionBackground(Color.WHITE);
		for(BundleInfo bundle:siteInfo.getBundles()){
			model.addRow(new Object[]{"",bundle.getId(),bundle.getVersion()});
		}
		JScrollPane scroll=new JScrollPane(table);
		scroll.setPreferredSize(getPreferredSize());
		add(scroll);
		int firstColWidth=50;
		table.getColumnModel().getColumn(0).setWidth(firstColWidth);
		table.getColumnModel().getColumn(0).setPreferredWidth(firstColWidth);
		table.getColumnModel().getColumn(0).setMaxWidth(firstColWidth);
	}

	public boolean isSelected(int row){
		Object value = table.getValueAt(row, 0);
		return SELECTED_VALUE.equals(value);
	}
	
	public void select(int row){
		table.setValueAt(SELECTED_VALUE, row, 0);
	}
	
	public void unselect(int row){
		table.setValueAt("", row, 0);
	}
	
	public int countSelected(){
		int rowCount = table.getRowCount();
		int total=0;
		for(int i=0;i<rowCount;i++){
			if(isSelected(i)){
				total++;
			}
		}
		return total;
	}

	public JTable getTable() {
		return table;
	}

	public String getBundleId(int row) {
		return table.getValueAt(row, 1).toString();
	}

	public String getBundleVersion(int row) {
		return table.getValueAt(row, 2).toString();
	}

	public String getSiteName() {
		return siteName;
	}

	public List<BundleInfo> getSimpleBundles() {
		int rowCount = table.getRowCount();
		List<BundleInfo> bundles=new ArrayList<BundleInfo>();
		for(int row=0;row<rowCount;row++){
			if(isSelected(row)){
				BundleInfo bundle=new BundleInfo();
				bundle.setId(getBundleId(row));
				bundle.setVersion(getBundleVersion(row));
				bundles.add(bundle);
			}
		}
		return bundles;
	}
	
}
